<?php
/*
 * @Author: lokei
 * @Date: 2022-10-24 23:37:22
 * @LastEditors: lokei
 * @LastEditTime: 2022-12-18 17:41:14
 * @Description: 
 */

namespace App\Http\Controllers\Cms\Comment;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Cms\Comment\CommentModel;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    public function list()
    {
        $items = CommentModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }
    public function submit(Request $request)
    {
        $user = app('auth')->user();
        if ($request->input('goods_ids') != null && $request->input('goods_ids') != '') {
            $comment = new CommentModel();
            $comment->uid = $user->uid;
            $comment->order_id = $request->input('order_id');
            $comment->goods_id = 0;
            $comment->content = $request->input('content');
            $comment->image = $request->input('image');
            $comment->avatar = $request->input('avatar');
            $comment->name = $request->input('name');
            $comment->mobile = $request->input('mobile');
            $comment->created_at = date('Y-m-d H:i:s', time());
            $comment->save();
            $goods_ids_str = $request->input('goods_ids');
            $goods_ids_arr = explode(',', $goods_ids_str);
            foreach ($goods_ids_arr as $goods_id) {
                $comment = new CommentModel();
                $comment->uid = $user->uid;
                $comment->order_id = 0;
                $comment->goods_id = $goods_id;
                $comment->star_value = $request->input('star_value');
                $comment->content = $request->input('content');
                $comment->image = $request->input('image');
                $comment->avatar = $request->input('avatar');
                $comment->name = $request->input('name');
                $comment->mobile = $request->input('mobile');
                $comment->created_at = date('Y-m-d H:i:s', time());
                $comment->save();
            }
        } else {
            $comment = new CommentModel();
            $comment->uid = $user->uid;
            $comment->order_id = $request->input('order_id');
            $comment->goods_id = $request->input('goods_id');
            $comment->star_value = $request->input('star_value');
            $comment->content = $request->input('content');
            $comment->image = $request->input('image');
            $comment->avatar = $request->input('avatar');
            $comment->name = $request->input('name');
            $comment->mobile = $request->input('mobile');
            $comment->created_at = date('Y-m-d H:i:s', time());
            $comment->save();
        }
        if($request->input('clerk_id') != null && intval($request->input('clerk_id')) > 0) {
            $comment = new CommentModel();
            $comment->uid = $user->uid;
            $comment->order_id = $request->input('order_id');
            $comment->clerk_id = $request->input('clerk_id');
            $comment->star_value = $request->input('star_value');
            $comment->content = $request->input('content');
            $comment->image = $request->input('image');
            $comment->avatar = $request->input('avatar');
            $comment->name = $request->input('name');
            $comment->mobile = $request->input('mobile');
            $comment->created_at = date('Y-m-d H:i:s', time());
            $comment->save();
        }
        return ResultTool::success();
    }

    public function ofOrder(Request $request)
    {
        $comment = CommentModel::where('order_id', '=', $request->input('order_id'))->first();
        $res = ResultTool::success();
        $res['data'] = $comment;
        return $res;
    }

    public function ofGoods(Request $request)
    {
        $items = CommentModel::where('goods_id', '=', $request->input('goods_id'))->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function ofTopic(Request $request)
    {
        if ($request->input('topic_id') != null && intval($request->input('topic_id')) > 0) {
            $items = CommentModel::where('topic_id', '=', $request->input('topic_id'))->get();
        } else {
            $items = [];
        }
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function delete(Request $request)
    {
        CommentModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
}
